// 职责是封装.vue文件上的js逻辑
import { ref } from 'vue'
import { getParkInfoAPI } from '@/api/BigScreen'

// 1. 请求服务器数据并渲染2d图标的4个数据
// 1.1 定义响应式数据
// 最终的结构：{base:{},parkIncome:{},parkIndustry:[]}
// base:园区概况
// parkIncome:园区收入
// parkIndustry:园区产业分布
const parkInfo = ref({})

// 1.2 请求服务器数据赋值给响应式数据
const getParkInfo = async () => {
  const res = await getParkInfoAPI()
  parkInfo.value = res.data
  console.log(res.data);
}


// 2. 封装系列成员完成园区收入柱状图的渲染
import * as echarts from 'echarts'

// 作用是用来获取echars渲染容器的dom对象的
const barChart = ref(null)

// 职责：负责获取parkInfo.parkIncome数据，调用echart的API，完成柱状图的渲染
const initBarChart = () => {
  // 1. 解构出parkIncome数据
  const { parkIncome } = parkInfo.value

  // 2. 准备echarts的参数
  const barOptions = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    grid: {
      // 让图表占满容器
      top: '10px',
      left: '0px',
      right: '0px',
      bottom: '0px',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        axisTick: {
          alignWithLabel: true,
          show: false,
        },
        data: parkIncome.xMonth,
      },
    ],
    yAxis: [
      {
        type: 'value',
        splitLine: {
          show: false,
        },
      },
    ],
    series: [
      {
        name: '园区年度收入',
        type: 'bar',
        barWidth: '10px',
        data: parkIncome.yIncome.map((item, index) => {
          const color =
            index % 2 === 0
              ? new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0.23, color: '#74c0f8' },
                { offset: 1, color: 'rgba(116,192,248,0.00)' },
              ])
              : new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0.23, color: '#ff7152' },
                { offset: 1, color: 'rgba(255,113,82,0.00)' },
              ])
          return { value: item, itemStyle: { color } }
        }),
      },
    ],
    textStyle: {
      color: '#B4C0CC',
    },
  }
// 3. setOption方法完成渲染
  const myBarChart = echarts.init(barChart.value)
  myBarChart.setOption(barOptions)
}

// 渲染园区产业分布图表
const pieChart = ref(null)
const initPieChart = () => {
  const { parkIndustry } = parkInfo.value
  const pieOption = {
    color: [
      '#00B2FF', '#2CF2FF', '#892CFF', '#FF624D', '#FFCF54', '#86ECA2'],
    legend: {
      itemGap: 20,
      bottom: '0',
      icon: 'rect',
      itemHeight: 10, // 图例icon高度
      itemWidth: 10, // 图例icon宽度
      textStyle: {
        color: '#c6d1db',
      },
    },
    tooltip: {
      trigger: 'item'
    },
    series: [
      {
        name: '园区产业分析',
        type: 'pie',
        radius: ['55%', '60%'], // 设置内圈与外圈的半径使其呈现为环形
        center: ['50%', '40%'], // 圆心位置， 用于调整整个图的位置
        tooltip: {
          trigger: 'item',
          formatter: (params) => {
            return `${params.seriesName}</br><div style='display:flex;justify-content: space-between;'><div>${params.marker}${params.name}</div><div>${params.percent}%</div></div>`
          }
        },
        label: {
          show: false,
          position: 'center',
        },
        data: parkIndustry,
      },
    ],

  }
  const myPieChart = echarts.init(pieChart.value)
  myPieChart.setOption(pieOption)
}




// 4.0 导出给.vue文件使用
export function useBigScreenMember() {
  return {
    parkInfo,
    getParkInfo,
    barChart,
    initBarChart,
    pieChart,
    initPieChart
  }
}
